package appapi.service.impl;

import appapi.Request.CompPageReq;
import appapi.Global.BusinessException;
import appapi.Request.CompReq;
import appapi.dao.CompanyMapper;
import appapi.dao.OrganizationMapper;
import appapi.entity.Zhongjiao.CompanyDto;
import appapi.entity.Zhongjiao.OrganizationDto;
import appapi.service.ICompanyService;
import appapi.service.IOrganization;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author djq
 * @date 2023/5/11 14:13
 * @description:
 */
@Component
public class CompanyService implements ICompanyService {
    @Resource
    private CompanyMapper companyMapper;

    @Autowired
    private OrganizationMapper organizationMapper;

    /**
     * 新增公司
     *
     * @param dto
     * @return
     */
    @Override
    public int InsertCompany(CompanyDto dto) {
        CompanyDto companyDto = companyMapper.selectByName(dto.getCompName());
        if (companyDto != null) throw new BusinessException(500, "该公司已存在");

        return companyMapper.insert(dto);

    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public int UpdateCompany(CompanyDto dto) {
        CompanyDto companyDto = companyMapper.selectByPrimaryKey(dto.getCompId());
        if (companyDto == null) throw new BusinessException(500, "该公司不存在或者已被删除!");

        return companyMapper.updateByPrimaryKeySelective(dto);
    }

    /**
     * 删除
     *
     * @param compId
     * @return
     */
    @Override
    public int DelCompany(Integer compId) {
        if (companyMapper.allowDelete(compId) > 0) {
            return -1;
        }

        return companyMapper.deleteByPrimaryKey(compId);
    }

    /**
     * 查询列表
     *
     * @param dto
     * @return
     */
    @Override
    public List<Map<String, Object>> SelectCompanyPage(CompPageReq dto) {
        PageHelper.startPage(dto.getCurrPage(), dto.getPageSize());

        return companyMapper.selectCompList(dto);
    }

    @Override
    public List<Map<String, Object>> GetCompanyCheckList(CompReq dto) {
        return companyMapper.selectCompCheckList(dto);
    }

    /**
     * 查询详情
     * @param compId
     * @return
     */
    @Override
     //    public Map<String, Object> SelectCompanyDetail = (Integer compId)-> companyMapper.selectCompDetail(compId);
    public Map<String, Object> SelectCompanyDetail(Integer compId){
        Map<String, Object> compDetail = companyMapper.selectCompDetail(compId);
        List<Long> orgaPathIds = Arrays.stream(compDetail.get("orgaPathIds").toString().split("-")).map(map->(Long.parseLong(map.trim()))).collect(Collectors.toList());
        compDetail.put("orgaArr",orgaPathIds);
        return compDetail;
    }

    @Override
    public List<CompanyDto> getList(CompPageReq req) {
        return  companyMapper.selectCompLists(req);
    }
}
